package com.manjason.property.listener;

import com.manjason.property.Property;
import com.manjason.property.util.Util;
import org.bukkit.entity.LivingEntity;
import org.bukkit.entity.Player;
import org.bukkit.entity.Projectile;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.entity.EntityDamageByEntityEvent;

public class PlayerListener implements Listener {

    public static void main (String [] args) {
        System.out.println(Math.pow(0.1667,100) / 100);
    }

    @EventHandler(priority = EventPriority.HIGHEST)
    public void onEntityDamageByPlayer (EntityDamageByEntityEvent e) {
        if ((e.getDamager() instanceof Player || e.getDamager() instanceof Projectile) && e.getEntity() instanceof LivingEntity) {
            Player player = null;
            if (e.getDamager() instanceof  Projectile) {
                player = ((Player)((Projectile)e.getDamager()).getShooter());
            } else if (e.getDamager() instanceof Player) {
                player = (Player) e.getDamager();
            }
            double damage = e.getDamage();
            com.manjason.property.entity.Property theProperty = Util.getPropertyByPlayerName(player.getName());
            if (theProperty != null) {
                //射击计算
                if (e.getDamager() instanceof Projectile) {
                    if (theProperty.getLuck() != 0) {
                        damage += theProperty.getLuck() + 0.2;
                    }
                }
                if (theProperty.getPhysical() != 0) {
                    //力量计算
                    damage += theProperty.getPhysical() + 1.33;
                }
                if (theProperty.getQuick() != 0) {
                    //敏捷计算
                    damage += theProperty.getQuick() + 0.2;
                }
                //物理暴击计算
                //物理暴击几率
                double physicalCriticalStrikeChance = theProperty.getLuck() * 0.0015;
                if (Math.random() < physicalCriticalStrikeChance) {
                    //物理暴击伤害
                    double physicalCriticalStrikeDamage = theProperty.getPhysical() * 0.02;
                    damage += physicalCriticalStrikeDamage;
                }
                //魔法暴击计算
                //魔法暴击几率
                double magicCriticalStrikeChance = theProperty.getLuck() * 0.0015;
                if (Math.random() < magicCriticalStrikeChance) {
                    //魔法暴击伤害
                    double magicCriticalStrikeDamage = theProperty.getIq() * 0.02;
                    damage += magicCriticalStrikeDamage;
                }
                if (e.getEntity() instanceof Player) {
                    Player targetPlayer = (Player) e.getEntity();
                    com.manjason.property.entity.Property targetProperty = Util.getPropertyByPlayerName(targetPlayer.getName());
                    if (targetProperty != null) {
                        //击中计算
                        if (targetProperty.getQuick() != 0) {
                            //回避
                            double dodge = targetProperty.getQuick() * 0.02;
                            //命中
                            double hit = theProperty.getQuick() * 0.02;
                            //回避几率
                            double range = dodge - hit;
                            //回避几率计算
                            if (range > 0) {
                                if (Math.random() < range) {
                                    e.setCancelled(true);
                                    return;
                                }
                            }
                        }
                        //魔法计算
                        if (targetProperty.getSpirit() != 0) {
                            //魔法伤害
                            double magicDamage = (theProperty.getSpirit() * 0.2) + (theProperty.getIq() * 1.33);
                            //魔法防御
                            double magicArmor = targetProperty.getSpirit() * 1;
                            damage += magicDamage - magicArmor;
                        }
                        //防御计算
                        if (targetProperty.getPhysique() != 0) {
                            //防御
                            double armor = targetProperty.getPhysique() * 1;
                            damage -= armor;
                        }
                    }
                }
            }
            if (damage <= 0) {
                e.setCancelled(true);
            } else {
                e.setDamage(damage);
            }
        }
    }



}
